<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">学期：</span>
      <el-select
        v-model="params.termName"
        placeholder="请输入或选择"
        clearable
        filterable
        @blur="selectBlur"
        @clear="selectClear"
        @change="selectChange"
      >
        <el-option
          v-for="(item, index) in terms"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
      <span class="search-label" style="margin-left: 20px">课程：</span>
      <el-select
        v-model="params.courseName"
        placeholder="请输入或选择"
        clearable
        filterable
        @blur="selectBlur2"
        @clear="selectClear2"
        @change="selectChange2"
      >
        <el-option
          v-for="(item, index) in courses"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
      <span class="search-label" style="margin-left: 20px">状态：</span>
      <el-select v-model="params.verification_status">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.text"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="search"
        >查询</el-button
      >
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button @click="batchDeleteCard">批量删除</el-button>
      <el-button @click="handleExportExcel">导出excel表</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="list"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="学期" prop="termName" />
        <el-table-column label="课程名" prop="courseName" />
        <el-table-column label="分数" prop="scoreValue" />
        <el-table-column
          label="状态"
          prop="verificationStatus"
          :formatter="formatStatus"
        />
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { getScoreAPI,exportExcel2API } from "@/api/score";
import { getTermsListByStudentAPI } from "@/api/term";
import { getCoursesListByStudentAPI } from "@/api/course";

export default {
  name: "Check_results",
  data() {
    return {
      terms: [],
      courses: [],
      // 表单参数
      statusList: [
        {
          text: "全部",
          value: null,
        },
        {
          text: "未审核",
          value: "0",
        },
        {
          text: "已通过",
          value: "1",
        },
        {
          text: "未通过",
          value: "2",
        },
        {
          text: "送审中",
          value: "3",
        }
      ],
      total: 0, //页总数
      list: [], //列表
      params: {
        //参数
        page: 1,
        pageSize: 2,
        termName: "",
        courseName: "",
        verification_status: null,
      },
    };
  },
  created() {
    this.getScore();
    this.getTermsListByStudent();
    this.getCoursesListByStudent();
  },
  methods: {
    handleExportExcel() {
      exportExcel2API(this.params)
        .then(response => {
          const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

          const link = document.createElement('a');
          link.href = window.URL.createObjectURL(blob);
          link.download = 'scores.xlsx';
          link.click();

          window.URL.revokeObjectURL(link.href);
        })
        .catch(error => {
          console.error('Failed to export Excel:', error);
        });
    },
    async getCoursesListByStudent(){
      const res = await getCoursesListByStudentAPI()
      this.courses = res.data
    },
    async getTermsListByStudent(){
      const res = await getTermsListByStudentAPI()
      this.terms = res.data
    },
    //多选框输入值
    selectBlur2(e) {
      // 意见类型
      if (e.target.value !== "") {
        this.params.courseName = e.target.value;
        this.$forceUpdate(); // 强制更新
      }
    },
    //清空多选框
    selectClear2() {
      this.params.courseName = "";
      this.$forceUpdate();
    },
    //多选框改变值
    selectChange2(val) {
      this.params.courseName = val;
      this.$forceUpdate();
    },
    //获取成绩
    async getScore() {
      const res = await getScoreAPI(this.params);
      this.total = res.data.total;
      this.list = res.data.records;
    },
    //多选框输入值
    selectBlur(e) {
      // 意见类型
      if (e.target.value !== "") {
        this.params.termName = e.target.value;
        this.$forceUpdate(); // 强制更新
      }
    },
    //清空多选框
    selectClear() {
      this.params.termName = "";
      this.$forceUpdate();
    },
    //改变多选框
    selectChange(val) {
      this.params.termName = val;
      this.$forceUpdate();
    },
    //点击查询按钮
    search() {
      this.params.page = 1;
      this.getScore();
    },
    //计算序号
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1;
    },
    //页数改变
    handleCurrentChange(val) {
      this.params.page = val;
      this.getScore();
    },
    //页条数改变
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getScore();
    },
    //格式化状态
    formatStatus(row) {
      const MAP = {
        0: "未审核",
        1: "已通过",
        2: "未通过",
        3: "送审中"
      };
      return MAP[row.verificationStatus];
    },
  },
};
</script>


<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>